package procuctconsumer;


import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

 class Print{
    Lock lock =new ReentrantLock();
    Condition c1 =lock.newCondition();
    Condition c2 =lock.newCondition();
    Condition c3 =lock.newCondition();
    volatile int numer =1;

    public void printC1(int j)  {
        lock.lock();
        System.out.println(Thread.currentThread().getName()+" 开始准备执行第"+j+"次打印");
        try {
            while (numer ==1){
                System.out.println(Thread.currentThread().getName()+" 开始执行第"+j+"次打印");
                for (int i=1;i<=5;i++){
                    System.out.println(Thread.currentThread().getName()+"打印"+i+"次");
                }
                numer=2;
                c2.signal();
            }
            c1.await();
        }catch (Exception exception){
            exception.printStackTrace();
        }finally {
            lock.unlock();
            System.out.println(Thread.currentThread().getName()+" 结束执行第"+j+"次打印");
        }
    }

    public void printC2(int j) {
        lock.lock();
        System.out.println(Thread.currentThread().getName()+" 开始准备执行第"+j+"次打印");
        try {
            while (numer ==2){
                System.out.println(Thread.currentThread().getName()+" 开始执行第"+j+"次打印");
                for (int i=1;i<=10;i++){
                    System.out.println(Thread.currentThread().getName()+"打印"+i+"次");
                }
                numer=3;
                c3.signal();
            }
            c2.await();
        }catch (Exception exception){
            exception.printStackTrace();
        }finally {
            lock.unlock();
            System.out.println(Thread.currentThread().getName()+" 结束执行第"+j+"次打印");
        }
    }

    public void printC3(int j) {
        lock.lock();
        System.out.println(Thread.currentThread().getName()+" 开始准备执行第"+j+"次打印");
        try {
            while (numer ==3){
                System.out.println(Thread.currentThread().getName()+" 开始执行第"+j+"次打印");
                for (int i=1;i<=15;i++){
                    System.out.println(Thread.currentThread().getName()+"打印"+i+"次");
                }
                numer=1;
                c1.signal();
            }
            c3.await();
        }catch (Exception exception){
            exception.printStackTrace();
        }finally {
            lock.unlock();
            System.out.println(Thread.currentThread().getName()+" 结束执行第"+j+"次打印");
        }
    }
}

public class condition {




    public static void main(String[] args) {
        Print print =new Print();

        new Thread(()->{
            for (int i=1;i<=10;i++){
                print.printC1(i);
            }
        },"c1").start();

        new Thread(()->{
            for (int i=1;i<=10;i++){
                print.printC2(i);
            }
        },"c2").start();


        new Thread(()->{
            for (int i=1;i<=10;i++){
                print.printC3(i);
            }
        },"c3").start();
    }
}
